package org.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;

public abstract class AbstractFrame  extends JComponent{
    protected String mPath;
    private static BufferedImage  defaultImage;
    protected BufferedImage image;
    static{
         InputStream is=Thread.currentThread().getContextClassLoader().getResourceAsStream("res/icon/addPreview.png");
         try {
            defaultImage=ImageIO.read(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public AbstractFrame() {
        this.setSize(this.getPreferredSize());
    }
    @Override
    protected void paintComponent(Graphics g) {
        if(image==null){
         g.drawImage(defaultImage,0,0,null);
        }else{
            g.drawImage(image,0,0,null);       
        }
        g.dispose();
    }
    public String getPath() {
        return mPath;
    }
    public void setPath(String path) {
        mPath = path;
        try {
            image=ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  class  PaperFrame extends AbstractFrame{
      
      @Override
    public Dimension getPreferredSize() {
        Dimension size=new Dimension();
        size.width=300;
        size.height=300;
        return size;
    }
  }
  class ThumbnailFrame extends AbstractFrame{
      @Override
      public Dimension getPreferredSize() {
          Dimension size=new Dimension();
          size.width=110;
          size.height=180;
          return size;
      }
  }
  class  PaperFrameWithButton extends JComponent{
      private PaperFrame mPaperFrame;
      private JButton mButton;
      public PaperFrameWithButton() {
          mPaperFrame=new PaperFrame();
          mButton=new JButton("替换壁纸");
          GridBagLayout   mGridBagLayout = new GridBagLayout();
          GridBagConstraints c = new GridBagConstraints();
          this.setLayout(mGridBagLayout);
          c.gridwidth = GridBagConstraints.REMAINDER;
          c.fill = GridBagConstraints.NONE;
          mGridBagLayout.setConstraints(mPaperFrame, c);
          this.add(mPaperFrame);
          c.insets = new Insets(5, 0, 0, 0);
          c.gridwidth = GridBagConstraints.RELATIVE;
          mGridBagLayout.setConstraints(mButton, c);
          this.add(mButton);
    }
      @Override
    public Dimension getPreferredSize() {
          Dimension size=new Dimension();
          size.width=300;
          size.height=350;
          return size;
    }
      
  }
   class  ThumbnailFrameWithButton extends JComponent{
       private PaperFrame mPaperFrame;
       private JButton mButton;
       public ThumbnailFrameWithButton() {
           mPaperFrame=new PaperFrame();
           mButton=new JButton("替换缩略图");
           GridBagLayout   mGridBagLayout = new GridBagLayout();
           GridBagConstraints c = new GridBagConstraints();
           this.setLayout(mGridBagLayout);
           c.gridwidth = GridBagConstraints.REMAINDER;
           c.fill = GridBagConstraints.NONE;
           mGridBagLayout.setConstraints(mPaperFrame, c);
           this.add(mPaperFrame);
           c.insets = new Insets(5, 0, 0, 0);
           c.gridwidth = GridBagConstraints.RELATIVE;
           mGridBagLayout.setConstraints(mButton, c);
           this.add(mButton);
     }
       @Override
    public Dimension getPreferredSize() {
           Dimension size=new Dimension();
           size.width=110;
           size.height=230;
           return size;
    }
   }
  
